home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / fcsrc.exe / FCDLGS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-06-04  |  14.2 KB  |  542 lines

  1. unit FCDLGS;
  2.  
  3. {$R FCDLGS}
  4.  
  5. interface
  6.  
  7. uses WinTypes, WinProcs, WObjects, WinDOS, Strings;
  8.  
  9. {$I fc.inc}
  10.  
  11. const
  12.     id_DirDlg                    = 400;
  13.     id_DirListbox            = 401;
  14.     id_DirPrompt            = 402;
  15.  
  16.     id_ExeDlg                    = 410;
  17.     id_ExePrompt            = 411;
  18.     id_ExeFilebox            = 412;
  19.     id_ExeDirbox            = 413;
  20.     id_ExeFilePrompt    = 414;
  21.  
  22.   id_SaveAsDlg            = 420;
  23.   id_Namebox                = 421;
  24.  
  25.   id_SctnDlg                 = 430;
  26.     id_SctnList             = 431;
  27.  
  28.   id_SetupDlg                = 440;
  29.   id_ItmEdit                = 441;
  30.   id_GrpCombo                = 442;
  31.   id_InstallBtn            = 443;
  32.  
  33. type
  34.     PDirDlg    = ^TDirDlg;
  35.     TDirDlg    = object(TDialog)
  36.         Buffer: PChar;
  37.         constructor Init(AParent: PWindowsObject; ABuffer: PChar);
  38.         procedure SetupWindow; virtual;
  39.         procedure MsgDirListbox(var Msg: TMessage);
  40.         virtual id_First + id_DirListbox;
  41.     procedure Ok(var Msg: TMessage); virtual id_First + id_Ok;
  42.      end;
  43.  
  44.     PExeDlg = ^TExeDlg;
  45.     TExeDlg = object(TDialog)
  46.         Buffer: PChar;
  47.     FileBox: PListBox;
  48.         constructor Init(AParent: PWindowsObject; ABuffer: PChar);
  49.         procedure SetupWindow; virtual;
  50.         procedure MsgExeFilebox(var Msg: TMessage);
  51.         virtual id_First + id_ExeFilebox;
  52.         procedure MsgExeDirbox(var Msg: TMessage);
  53.         virtual id_First + id_ExeDirbox;
  54.     procedure Ok(var Msg: TMessage);
  55.         virtual id_First + id_Ok;
  56.     end;
  57.  
  58.   PSaveAsDlg = ^TSaveAsDlg;
  59.   TSaveAsDlg = object(TDialog)
  60.       Buffer: PChar;
  61.     NameBox: PEdit;
  62.     constructor Init(AParent: PWindowsObject; ABuffer: PChar);
  63.     procedure SetupWindow; virtual;
  64.     function CanClose: Boolean; virtual;
  65.   end;
  66.  
  67. type
  68.     PSctnDlg = ^TSctnDlg;
  69.     TSctnDlg = object(TDialog)
  70.     Buffer: PChar;
  71.     BufferSize: Word;
  72.     SctnList: PListbox;
  73.     OkBtn: PButton;
  74.     constructor Init(AParent: PWindowsObject; ABuffer: PChar;
  75.         ABufferSize: Word);
  76.     function CanClose: Boolean; virtual;
  77.         procedure SetupWindow; virtual;
  78.         procedure CMSctnList(var Msg: TMessage);
  79.         virtual id_First + id_SctnList;
  80.   end;
  81.  
  82.     PPmGroup = ^TPmGroup;
  83.     TPmGroup = object(TObject)
  84.         Filename: Array[0..100] of Char;
  85.         Title   : Array[0..100] of Char;
  86.         constructor Init;
  87.         procedure NextGroup(AFilename: PChar);
  88.     end;
  89.  
  90.   PInstallDlg = ^TInstallDlg;
  91.   TInstallDlg = object(TDialog)
  92.       SectionName: PChar;
  93.     TheItm, TheGrp: PChar;
  94.       ItmEdit: PEdit;
  95.     GrpCombo: PCombobox;
  96.     ServerWindow: HWnd;
  97.     PendingMessage: Word;
  98.       constructor Init(AParent: PWindowsObject;
  99.         ASectionName, AnItm, AGrp: PChar);
  100.     procedure SetupWindow; virtual;
  101.     procedure CMInstall(var Msg: TMessage);
  102.         virtual id_First + id_InstallBtn;
  103.     procedure WMDDEAck(var Msg: TMessage);
  104.         virtual wm_First + wm_DDE_Ack;
  105.     procedure WMDDETerminate(var Msg: TMessage);
  106.         virtual wm_First + wm_DDE_Terminate;
  107.     procedure WMDestroy(var Msg: TMessage);
  108.         virtual wm_First + wm_Destroy;
  109.     procedure InitiateDDE;
  110.     procedure TerminateDDE;
  111.   end;
  112.  
  113. implementation
  114.  
  115. { ----- TDirDlg methods ---------------------------------------------- }
  116.  
  117. constructor TDirDlg.Init(AParent: PWindowsObject; ABuffer: PChar);
  118. var
  119.     AControl: PControl;
  120. begin
  121.     TDialog.Init(AParent, PChar(id_DirDlg));
  122.   Buffer := ABuffer;
  123.     AControl := New(PListBox, InitResource(@Self, id_DirListbox));
  124.   AControl := New(PStatic, InitResource(@Self, id_DirPrompt,
  125.       fsPathName+1));
  126. end;
  127.  
  128. procedure TDirDlg.SetupWindow;
  129. begin
  130.     DlgDirList(HWindow, GetCurDir(Buffer, 0), id_DirListbox,
  131.       id_DirPrompt, $C010);
  132. end;
  133.  
  134. procedure TDirDlg.MsgDirListbox(var Msg: TMessage);
  135. begin
  136.     if Msg.LParamHi = lbn_DblClk then
  137.     begin
  138.         DlgDirSelect(HWindow, Buffer, id_DirListbox);
  139.         FileExpand(Buffer, Buffer);
  140.         DlgDirList(HWindow, Buffer, id_DirListbox, id_DirPrompt, $C010);
  141.     end;
  142. end;
  143.  
  144. procedure TDirDlg.Ok(var Msg: TMessage);
  145. var
  146.     Len: Integer;
  147. begin
  148.     Buffer[0] := #0;
  149.     DlgDirSelect(HWindow, Buffer, id_DirListbox);
  150.   if StrIComp(Buffer, '') = 0 then
  151.   begin
  152.       FileExpand(Buffer, Buffer);
  153.       Len := StrLen(Buffer) - 1;
  154.       if Buffer[Len] = '\' then
  155.           Buffer[Len] := #0;
  156.       TDialog.Ok(Msg);
  157.   end
  158.   else
  159.   begin
  160.         FileExpand(Buffer, Buffer);
  161.         DlgDirList(HWindow, Buffer, id_DirListbox, id_DirPrompt, $C010);
  162.     end;
  163. end;
  164.  
  165. { ---- TExeDlg methods -------------------------------------------------- }
  166.  
  167. constructor TExeDlg.Init(AParent: PWindowsObject; ABuffer: PChar);
  168. var
  169.     AControl: PControl;
  170. begin
  171.     TDialog.Init(AParent, PChar(id_ExeDlg));
  172.     Buffer := ABuffer;
  173.   FileBox := New(PListBox, InitResource(@Self, id_ExeFilebox));
  174.     AControl := New(PListBox, InitResource(@Self, id_ExeDirbox));
  175.     AControl := New(PStatic, InitResource(@Self, id_ExePrompt,
  176.       fsPathName+1));
  177.   AControl := New(PStatic, InitResource(@Self, id_ExeFilePrompt,
  178.       fsPathName+1));
  179. end;
  180.  
  181. procedure TExeDlg.SetupWindow;
  182. var
  183.     FileSpec: array[0..10] of Char;
  184. begin
  185.     DlgDirList(HWindow, GetCurDir(Buffer, 0), id_ExeDirbox, id_ExePrompt,
  186.       $C010);
  187.     DlgDirList(HWindow, '*.exe', id_ExeFilebox, id_ExeFilePrompt, $0000);
  188.     StrCopy(FileSpec, '*.com');
  189.     SendDlgItemMessage(HWindow, id_ExeFilebox, LB_DIR, $0000,
  190.       Longint(@FileSpec));
  191.     StrCopy(FileSpec, '*.bat');
  192.     SendDlgItemMessage(HWindow, id_ExeFilebox, LB_DIR, $0000,
  193.       Longint(@FileSpec));
  194.     StrCopy(FileSpec, '*.pif');
  195.     SendDlgItemMessage(HWindow, id_ExeFilebox, LB_DIR, $0000,
  196.       Longint(@FileSpec));
  197. end;
  198.  
  199. procedure TExeDlg.MsgExeDirbox(var Msg: TMessage);
  200. begin
  201.     if Msg.LParamHi = lbn_DblClk then
  202.       TExeDlg.Ok(Msg);
  203. end;
  204.  
  205. procedure TExeDlg.MsgExeFilebox(var Msg: TMessage);
  206. begin
  207.     if Msg.LParamHi = lbn_DblClk then
  208.         TExeDlg.Ok(Msg);
  209. end;
  210.  
  211. procedure TExeDlg.Ok(var Msg: TMessage);
  212. var
  213.     FileSpec: array[0..10] of Char;
  214.     Len: Integer;
  215. begin
  216.     Buffer[0] := #0;
  217.   DlgDirSelect(HWindow, Buffer, id_ExeDirbox);
  218.   if StrIComp(Buffer, '') > 0 then
  219.   begin
  220.         FileExpand(Buffer, Buffer);
  221.         DlgDirList(HWindow, Buffer, id_ExeDirbox, id_ExePrompt, $C010);
  222.         DlgDirList(HWindow, '*.exe', id_ExeFilebox, id_ExeFilePrompt, $0000);
  223.         StrCopy(FileSpec, '*.com');
  224.         SendDlgItemMessage(HWindow, id_ExeFilebox, LB_DIR, $0000, Longint(@FileSpec));
  225.         StrCopy(FileSpec, '*.bat');
  226.         SendDlgItemMessage(HWindow, id_ExeFilebox, LB_DIR, $0000, Longint(@FileSpec));
  227.         StrCopy(FileSpec, '*.pif');
  228.         SendDlgItemMessage(HWindow, id_ExeFilebox, LB_DIR, $0000, Longint(@FileSpec));
  229.     end
  230.   else
  231.   begin
  232.       DlgDirSelect(HWindow, Buffer, id_ExeFilebox);
  233.     if StrIComp(Buffer, '') > 0 then
  234.     begin
  235.         FileExpand(Buffer, Buffer);
  236.         Len := StrLen(Buffer) - 1;
  237.         if Buffer[Len] = '\' then
  238.             Buffer[Len] := #0;
  239.         TDialog.Ok(Msg);
  240.     end;
  241.   end;
  242. end;
  243.  
  244. { ---- TSaveAsDlg methods ---------------------------------------------- }
  245.  
  246. constructor TSaveAsDlg.Init(AParent: PWindowsObject; ABuffer: PChar);
  247. begin
  248.     TDialog.Init(AParent, PChar(id_SaveAsDlg));
  249.   Buffer := ABuffer;
  250.     NameBox := New(PEdit, InitResource(@Self, id_Namebox, PrgManItm+1));
  251. end;
  252.  
  253. procedure TSaveAsDlg.SetupWindow;
  254. begin
  255.     TDialog.SetupWindow;
  256.   NameBox^.Insert(Buffer);
  257. end;
  258.  
  259. function TSaveAsDlg.CanClose: Boolean;
  260. begin
  261.     NameBox^.GetText(Buffer, PrgManItm+1);
  262.   if StrIComp(Buffer, ' ') < 1 then
  263.       CanClose := FALSE
  264.   else
  265.       CanClose := TRUE;
  266. end;
  267.  
  268. { ---- TSctnDlg methods ------------------------------------------------ }
  269.  
  270. constructor TSctnDlg.Init(AParent: PWindowsObject; ABuffer: PChar;
  271.     ABufferSize: Word);
  272. begin
  273.     TDialog.Init(AParent, PChar(id_SctnDlg));
  274.   Buffer := ABuffer;
  275.   BufferSize := ABufferSize;
  276.     SctnList := New(PListbox, InitResource(@Self, id_SctnList));
  277.   OkBtn := New(PButton, InitResource(@Self, id_Ok));
  278. end;
  279.  
  280. procedure TSctnDlg.SetupWindow;
  281. var
  282.     AFile: Text;
  283.   FullIniName: array[0..fsPathName] of Char;
  284.     Buf, OutBuf: array[0..160] of Char;
  285.     i, Len: Integer;
  286.   SectionsFound: boolean;
  287. begin
  288.     TDialog.SetupWindow;
  289.   GetWindowsDirectory(FullIniName, fsPathName+1);
  290.   StrLCat(FullIniName, '\', fsPathName+1);
  291.   StrLCat(FullIniName, IniName, fsPathName+1);
  292.   {$I-}
  293.     assign(AFile, FullIniName);
  294.     Reset(AFile);
  295.   {$I+}
  296.   if IOResult <> 0 then
  297.   begin
  298.       StrLCopy(Buf, 'File Clerk could not find ', 160);
  299.     StrLCat(Buf, StrUpper(IniName), 160);
  300.     StrLCat(Buf, '. It made a new copy in the Windows directory.', 160);
  301.       MessageBox(HWindow, Buf, 'File error', mb_IconExclamation or mb_Ok);
  302.     Rewrite(AFile);
  303.     EndDlg(id_Cancel);
  304.     exit;
  305.   end;
  306.   SectionsFound := FALSE;
  307.     while not Eof(AFile) do
  308.     begin
  309.         Readln(AFile, Buf);
  310.         Len := StrLen(Buf);
  311.         if (Buf[0]='[') and (Buf[Len-1]=']') then
  312.     begin
  313.             SctnList^.AddString(StrLower(
  314.                 StrLCopy(OutBuf, @Buf[1], Len-2)));
  315.       SectionsFound := TRUE;
  316.     end;
  317.     end;
  318.     Close(AFile);
  319.     EnableWindow(OkBtn^.HWindow, SectionsFound);
  320. end;
  321.  
  322. procedure TSctnDlg.CMSctnList(var Msg: TMessage);
  323. begin
  324.     if Msg.LParamHi = lbn_DblClk then
  325.         TSctnDlg.Ok(Msg);
  326. end;
  327.  
  328. function TSctnDlg.CanClose: Boolean;
  329. begin
  330.     if SctnList^.GetSelIndex >= 0 then
  331.     begin
  332.         SctnList^.GetSelString(Buffer, BufferSize);
  333.         CanClose := True;
  334.     end
  335.   else CanClose := False;
  336. end;
  337.  
  338. { ---- TPmGroup methods ------------------------------------------------ }
  339.  
  340. constructor TPmGroup.Init;
  341. begin
  342.     TObject.Init;
  343. end;
  344.  
  345. procedure TPmGroup.NextGroup(AFilename: PChar);
  346. var Fp: File;
  347.     WOffset : Word;
  348.  
  349.   Procedure ReadStr(S: PChar);
  350.     var I:Integer;
  351.         Ch: Char;
  352.     begin
  353.        I := 0;
  354.        Repeat
  355.           BlockRead(Fp, Ch, 1);  { Read next character }
  356.           S[I] := Ch;
  357.           inc(I)
  358.        Until Ch = #0
  359.     end;
  360.  
  361. begin
  362.    Assign(Fp, AFilename);
  363.    {$I-} Reset(Fp, 1); {$I+}
  364.    if IOResult = 0 then
  365.    begin
  366.       StrCopy(Filename, AFilename);
  367.       Seek(Fp, $16);                  { Go to offset to Group Title }
  368.       BlockRead(Fp, WOffset, 2);      { Read the Offset }
  369.       Seek(Fp, WOffset);
  370.       ReadStr(Title);             { Read a Null Terminated String }
  371.             Close(Fp)
  372.    end
  373. end;
  374.  
  375. { ---- TInstallDlg methods -------------------------------------------- }
  376.  
  377. constructor TInstallDlg.Init(AParent: PWindowsObject;
  378.     ASectionName, AnItm, AGrp: PChar);
  379. begin
  380.     TDialog.Init(AParent, PChar(id_SetupDlg));
  381.   SectionName := ASectionName;
  382.   TheItm := AnItm;
  383.   TheGrp := AGrp;
  384.   ItmEdit := New(PEdit, InitResource(@Self, id_ItmEdit, PrgManItm+1));
  385.   GrpCombo := New(PCombobox, InitResource(@Self, id_GrpCombo, PrgManGrp+1));
  386.   ServerWindow := 0;
  387.   PendingMessage := 0;
  388. end;
  389.  
  390. procedure TInstallDlg.SetupWindow;
  391.  
  392.   procedure GetGroups;
  393.   type
  394.       TBuffer  = Array[0..1023] of Char;
  395.     PBuffer  = ^TBuffer;
  396.   var
  397.       Group         : PPMGroup;
  398.     BP             : PChar;
  399.       Filename : Array[0..98] of char;
  400.       I        : Integer;
  401.  
  402.   begin
  403.      Group := New(PPMGroup, Init);
  404.      GetMem(BP, Sizeof(TBuffer));
  405.      if BP <> NIL then
  406.      begin
  407.          GetPrivateProfileString('GROUPS', NIL, NIL,
  408.                  BP, sizeof(TBuffer)-1, 'PROGMAN.INI');
  409.          I := 0;
  410.          While BP[i] <> #0 do
  411.          begin
  412.                 GetPrivateProfileString('GROUPS', @BP[i], NIL,
  413.                     Filename, sizeof(Filename)-1, 'PROGMAN.INI');
  414.           Group^.NextGroup(Filename);
  415.           GrpCombo^.AddString(Group^.Title);
  416.                 inc(I, 1+StrLen(@BP[I]))
  417.          end;
  418.        FreeMem(BP, Sizeof(TBuffer));
  419.      end;
  420.   end;
  421.  
  422. begin
  423.    TDialog.SetupWindow;
  424.    GetGroups;
  425.    GrpCombo^.SetSelIndex(0);
  426.    ItmEdit^.SetText(SectionName);
  427.    InitiateDDE;
  428. end;
  429.  
  430. procedure TInstallDlg.CMInstall(var Msg: TMessage);
  431. const
  432.   sCreateGroup = '[CreateGroup(%s)]';
  433.   sAddItem = '[AddItem(%s, %s)]';
  434. type
  435.     CmdArray = array[0..1] of PChar;
  436. var
  437.   Executed: Boolean;
  438.   I, L: Integer;
  439.   HCommands: THandle;
  440.   PGrp, PCmd, PItm, PCommands: PChar;
  441.   GrpName, ItmName: array[0..63] of Char;
  442.   CmdName: array[0..fsPathName+63] of Char;
  443.   CmdAr: CmdArray;
  444. begin
  445.   GrpCombo^.GetSelString(GrpName, Sizeof(GrpName)-1);
  446.   ItmEdit^.GetText(ItmName, Sizeof(ItmName)-1);
  447.   StrPCopy(CmdName, ParamStr(0));
  448.   StrCat(CmdName, ' ');
  449.   StrCat(CmdName, SectionName);
  450.   CmdAr[0] := CmdName;
  451.   CmdAr[1] := ItmName;
  452.   Executed := False;
  453.   if (ServerWindow <> 0) and (PendingMessage = 0) then
  454.   begin
  455.     L := StrLen(GrpName) + (Length(sCreateGroup) - 1) +
  456.       StrLen(ItmName) + StrLen(CmdName) + (Length(sAddItem) - 1);
  457.     HCommands := GlobalAlloc(gmem_Moveable or gmem_DDEShare, L);
  458.     if HCommands <> 0 then
  459.     begin
  460.       PCommands := GlobalLock(HCommands);
  461.       PGrp := GrpName;
  462.       WVSPrintF(PCommands, sCreateGroup, PGrp);
  463.       PCommands := StrEnd(PCommands);
  464.       PCmd := CmdName;
  465.       PItm := ItmName;
  466.       WVSPrintF(PCommands, sAddItem, CmdAr[0]);
  467.       GlobalUnlock(HCommands);
  468.       if PostMessage(ServerWindow, wm_DDE_Execute, HWindow,
  469.         MakeLong(0, HCommands)) then
  470.       begin
  471.         PendingMessage := wm_DDE_Execute;
  472.         Executed := True;
  473.         StrCopy(TheItm, ItmName);
  474.         StrCopy(TheGrp, GrpName);
  475.       end else GlobalFree(HCommands);
  476.     end;
  477.     if not Executed then
  478.       MessageBox(HWindow, 'Program Manager DDE execute failed.',
  479.         'Error', mb_IconExclamation or mb_Ok);
  480.   end;
  481.   TDialog.Ok(Msg);
  482. end;
  483.  
  484. procedure TInstallDlg.WMDDEAck(var Msg: TMessage);
  485. begin
  486.   case PendingMessage of
  487.     wm_DDE_Initiate:
  488.       begin
  489.         if ServerWindow = 0 then
  490.           ServerWindow := Msg.WParam
  491.         else
  492.           PostMessage(Msg.WParam, wm_DDE_Terminate, HWindow, 0);
  493.         GlobalDeleteAtom(Msg.LParamLo);
  494.         GlobalDeleteAtom(Msg.LParamHi);
  495.       end;
  496.     wm_DDE_Execute:
  497.       begin
  498.         GlobalFree(Msg.LParamHi);
  499.         PendingMessage := 0;
  500.         SetFocus(HWindow);
  501.       end;
  502.   end;
  503. end;
  504.  
  505. procedure TInstallDlg.WMDDETerminate(var Msg: TMessage);
  506. begin
  507.   if Msg.WParam = ServerWindow then TerminateDDE;
  508. end;
  509.  
  510. procedure TInstallDlg.WMDestroy(var Msg: TMessage);
  511. begin
  512.   TerminateDDE;
  513.   TDialog.WMDestroy(Msg);
  514. end;
  515.  
  516. procedure TInstallDlg.InitiateDDE;
  517. var
  518.   AppAtom, TopicAtom: TAtom;
  519. begin
  520.   PendingMessage := wm_DDE_Initiate;
  521.   AppAtom := GlobalAddAtom('PROGMAN');
  522.   TopicAtom := GlobalAddAtom('PROGMAN');
  523.   SendMessage(HWnd(-1), wm_DDE_Initiate, HWindow,
  524.     MakeLong(AppAtom, TopicAtom));
  525.   GlobalDeleteAtom(AppAtom);
  526.   GlobalDeleteAtom(TopicAtom);
  527.   PendingMessage := 0;
  528.   if ServerWindow = 0 then
  529.     MessageBox(HWindow, 'Cannot establish DDE link to Program Manager.',
  530.       'Error', mb_IconExclamation or mb_Ok);
  531. end;
  532.  
  533. procedure TInstallDlg.TerminateDDE;
  534. var
  535.   W: HWnd;
  536. begin
  537.   W := ServerWindow;
  538.   ServerWindow := 0;
  539.   if IsWindow(W) then PostMessage(W, wm_DDE_Terminate, HWindow, 0);
  540. end;
  541.  
  542. end.